package com.hxht.cmp.controller;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.common.enums.ResultCode;
import com.hxht.cmp.common.result.ResultResponse;
import com.hxht.cmp.entity.Court;
import com.hxht.cmp.service.CasePlanService;
import com.hxht.cmp.service.CaseReviewService;
import com.hxht.cmp.service.StatisticalService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author alice on 2017/6/29 0029.
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private StatisticalService statisticalService;
    @Autowired
    private CasePlanService casePlanService;
    @Autowired
    private CaseReviewService caseReviewService;


    /**
     * 根据法院id请求自身以及所有的子法院
     *
     * @param courtId 法院
     * @return ResultResponse
     */
    @RequestMapping("index/initNavBar")
    @ResponseBody
    public ResultResponse initNavBar(String courtId) {
        Court court = Constants.COURT_MAP.get(courtId);
        if (ObjectUtils.isEmpty(court)) {
            return ResultResponse.failed(ResultCode.SEARCH_NULL);
        }
        List<Court> courtList = new ArrayList<>();
        int level = court.getLevel();
        String tempCourtStart = court.getCode().substring(0, level);
        for (Court tempCourt : Constants.COURT_LIST) {
            if (Objects.equals(tempCourt.getCode().substring(0, level), tempCourtStart)) {
                courtList.add(tempCourt);
            }
        }
        if (!courtList.isEmpty()) {
            return ResultResponse.success(courtList);
        } else {
            return ResultResponse.failed(ResultCode.SEARCH_NULL);
        }
    }


    /**
     * 首页顶部统计信息
     *
     * @param courtUid
     * @return
     */
    @RequestMapping("/selectIndexTopStatistical")
    @ResponseBody
    public ResultResponse selectListTopStatistical(String courtUid) {
        if (StringUtils.isBlank(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        HashMap<String, Integer> map = new HashMap<>();
        //全省法庭数量、科技法庭数、平台接入数
        Map<String, Integer> countMap = statisticalService.getCountCourtMap(courtUid);
        //获取正在开庭数
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        int countCourtNowTotal = statisticalService.getCountCourtByStatus(courtUid, 1, today);
        int countCourtNowTotal = casePlanService.getOpenNow(courtUid, today, "1");
        //统计当前法院及其下级法院的排期数量
        int countPlan = statisticalService.getCountCourtByStatus(courtUid, 0, null);
        //开庭总数
        int countOpenCourtNum = statisticalService.getCountOpenCourtNum(courtUid);
        //开庭总时长
        int countOpenCourtTime = statisticalService.getCountOpenCourtTime(courtUid);
        //总容量(单位T)
        int countFileSize = statisticalService.getCountFileSize(courtUid);
        //总法庭
        map.put("courtNum", countMap.get("allCourtNum"));
        //科技法庭
        map.put("technologyCourtTotal", countMap.get("technicalCourtNum"));
        map.put("countCourtNowTotal", countCourtNowTotal);
        map.put("countPlan", countPlan);
        map.put("countOpenCourtNum", countOpenCourtNum);
        map.put("countOpenCourtTime", countOpenCourtTime);
        map.put("countFileSize", countFileSize);
        //平台接入
        map.put("thirdCourtNum", countMap.get("thirdCourtNum"));
        return ResultResponse.success(map);
    }

    /**
     * 法官的开庭时长与开庭数
     *
     * @param courtUid
     * @return
     */
    @RequestMapping("/selectJudgeOpenTimeAndOpenNum")
    @ResponseBody
    public ResultResponse selectListJudgeOpenTimeAndOpenNum(String courtUid, String startDate, String endDate) {
        if (StringUtils.isBlank(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        List<Map<String, Object>> timeAndNumList = statisticalService.selectListJudgeOpenTimeAndOpenNum(courtUid, startDate, endDate);
        return ResultResponse.success(timeAndNumList);
    }

    /**
     * 查询今日开庭或明日预告
     *
     * @param courtUid
     * @param date
     * @param status   (用来表示只查询自身法院还是查询自身及其下级法院信息
     *                 (“0”表示自身,“1”表示自身及其下级法院))
     * @return
     */
    @RequestMapping("/selectListCasePlan")
    @ResponseBody
    public ResultResponse selectListCasePlan(String courtUid, String date, String status) {
        if (StringUtils.isBlank(courtUid) || StringUtils.isBlank(date)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        List<Map<String, Object>> mapList = casePlanService.selectListCasePlan(courtUid, date, status);
        return ResultResponse.success(mapList);
    }

    /**
     * 根据案件Id查询案件信息
     *
     * @param planUid
     * @return
     */
    @RequestMapping("/getCaseInfoByUid")
    @ResponseBody
    public ResultResponse getCaseInfoByUid(String planUid) {
        if (StringUtils.isBlank(planUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        try {
            Map<String, Object> map = casePlanService.getCaseInfoByUid(planUid);
            return ResultResponse.success(map);
        } catch (Exception e) {
            return ResultResponse.failed();
        }
    }

    @RequestMapping("/initSumCount")
    @ResponseBody
    public ResultResponse initSumCount(String courtUid, String startDate, String endDate, String status) {
        Map<String, Object> result = new HashedMap();
        if (ObjectUtils.isEmpty(courtUid) || ObjectUtils.isEmpty(startDate) || ObjectUtils.isEmpty(endDate) || ObjectUtils.isEmpty(status)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        result.put("lags", casePlanService.getCoordinate(courtUid));
        result.put("nowOpen", casePlanService.getOpenNow(courtUid, endDate, status));
        List<Map<String, Object>> caseCount = caseReviewService.getCaseCount(courtUid, null, startDate, endDate, status);
        result.put("sumOpenGroup", caseCount);
        result.put("countTime", caseReviewService.getCaseTimeCount(courtUid, null, startDate, endDate, status));
        result.put("technologyCount", caseReviewService.getTechnologyCourtRate(courtUid));
        List<Map<String, Object>> sumOpen = new ArrayList<>();
        //先做一个map模型
        List<Map<String, String>> allCaseType = caseReviewService.getAllCaseType();
        Map<String, Object> sumOpenMap = new HashMap<>();
        for (Map<String, String> caseType : allCaseType) {
            sumOpenMap.put(caseType.get("uid"), 0);
        }
        for (Map<String, Object> caseCountTemp : caseCount) {
            for (Map<String, Object> dataTemp : (List<Map<String, Object>>) caseCountTemp.get("data")) {
                sumOpenMap.put((String) dataTemp.get("caseType"), (int) sumOpenMap.get(dataTemp.get("caseType")) + ((Long) dataTemp.get("count")).intValue());
            }
        }
        for (Map.Entry entry : sumOpenMap.entrySet()) {
            Map<String, Object> map = new HashMap<>();
            for (Map<String, String> caseType : allCaseType) {
                if (Objects.equals(caseType.get("uid"), entry.getKey())) {
                    map.putAll(caseType);
                    map.put("count", entry.getValue());
                    sumOpen.add(map);
                }
            }
        }
        result.put("sumOpen", sumOpen);
        return ResultResponse.success(result);
    }

    @RequestMapping("countCountNow")
    @ResponseBody
    public ResultResponse countCountNow(String courtUid, String today, String status) {
        if (ObjectUtils.isEmpty(courtUid) || ObjectUtils.isEmpty(today) || ObjectUtils.isEmpty(status)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(casePlanService.getOpenNow(courtUid, today, status));
    }
}
